package com.gcit.seniorWish.web.modular.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.web.common.constant.factory.PageFactory;
import com.gcit.seniorWish.web.core.annotion.BussinessLog;
import com.gcit.seniorWish.web.core.annotion.Permission;
import com.gcit.seniorWish.web.common.constant.dictmap.DictMap;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.common.persistence.dao.DictMapper;
import com.gcit.seniorWish.web.common.persistence.model.Dict;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.exception.GunsException;
import com.gcit.seniorWish.web.core.log.LogObjectHolder;
import com.gcit.seniorWish.web.modular.system.dao.DictDao;
import com.gcit.seniorWish.web.modular.system.service.IDictService;
import com.gcit.seniorWish.web.modular.system.warpper.DictWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 字典控制器
 *
 * @author fengshuonan
 * @Date 2017年4月26日 12:55:31
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private String PREFIX = "/system/dict";

    @Resource
    private DictDao dictDao;

    @Resource
    private DictMapper dictMapper;

    @Resource
    private IDictService dictService;

    /**
     * 跳转到字典管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dict.html";
    }

    /**
     * 跳转到添加字典
     */
    @RequestMapping("/dictAdd")
    public String dictAdd() {
        return PREFIX + "/dictAdd.html";
    }

    /**
     * 跳转到修改字典
     */
    @Permission
    @RequestMapping("/dictEdit/{dictId}")
    public String dictEdit(@PathVariable String dictId, Model model) {
        Dict dict = dictMapper.selectById(dictId);
        model.addAttribute("dict", dict);
        List<Dict> subDicts = dictMapper.selectList(new EntityWrapper<Dict>().eq("pid", dictId).orderBy("num"));
        model.addAttribute("subDicts", subDicts);
        LogObjectHolder.me().set(dict);
        return PREFIX + "/dictInfo.html";
    }

    /**
     * 新增字典
     *
     * @param pDict 父级字典
     */
    @BussinessLog(value = "添加字典记录", key = "dictName,dictValues", dict = DictMap.class)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(@RequestBody Dict pDict) {
        if (pDict==null||StrUtil.isBlank(pDict.getName())||StrUtil.isBlank(pDict.getCode())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.dictService.addDict(pDict, pDict.getSubDictList());
        return SUCCESS_TIP;
    }

    /**
     * 获取所有字典列表
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<Dict> ew = new EntityWrapper<>();
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        if (StrUtil.isNotBlank(condition)) {
            ew.andNew().like("name",condition).or().like("code",condition).andNew();
        }
        //只查询根字典
        ew.eq("pid","0");
        page = this.dictService.selectMapsPage(page,ew);
        page.setRecords((List<Map<String, Object>>) new DictWarpper(page.getRecords()).warp());
        return packForBT(page);
    }

    /**
     * 修改字典
     */
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(@RequestBody Dict pDict) {
        if (pDict==null||StrUtil.isBlank(pDict.getName())||StrUtil.isBlank(pDict.getCode())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        dictService.editDict(pDict, pDict.getSubDictList());
        return super.SUCCESS_TIP;
    }

    /**
     * 删除字典记录
     */
    @BussinessLog(value = "删除字典记录", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(@RequestParam String dictId) {
        //缓存被删除的名称
        LogObjectHolder.me().set(ConstantFactory.me().getDictName(dictId));
        this.dictService.deleteDict(dictId);
        return SUCCESS_TIP;
    }

}
